package cn.whdream.datastructure.stack;

/**
 * 
 * @author admin 个,十位数字计算器
 */
public class TwoPlacesCalculater {
	
	public static void main(String[] args) {
		//String expression = "7+2*6-4";
		String expression = "70+2*6-4";//如何处理多维数组
		//定义 数字栈
		ArrayStack numStack = new ArrayStack(10);
		//定义符号栈
		ArrayStack operStack = new ArrayStack(10);
		//声明变量缓存祥光数据
		int num1 = 0;
		int num2 = 0;
		int oper;
		int res = 0;
		char ch;
		for (int i = 0; i < expression.length(); i++) {
			//扫描表达式
			ch = expression.substring(i, i + 1).charAt(0);
			//判断是否为 符号
			if (isOpertion(ch)) {
				//判断 符号栈是否为空
				if (!operStack.isEmpty()) {
					//不为空，获取栈顶元素
					char head = (char) operStack.peek();
					//对比是否需要出栈计算
					boolean neadPop =needPopOpertion(head,ch);
					if(neadPop) {
						//需要出栈，则将对应的 数字，操作符进行处理 计算
						oper = operStack.pop();
						num1 = numStack.pop();
						num2 = numStack.pop();
						res = calculate(num1,num2,oper);
						//结果进入数字栈
						numStack.add(res);
						//扫描到的 操作符进入操作符栈
						operStack.add(ch);
					}else {
						//空，则直接入栈
						operStack.add(ch);
					}
					
				}else {
					//为空则添加 ch
					operStack.add(ch);
				}
			}else {
				//扫描到数字不能直接入栈
				//numStack.add(ch-48);
				//初始化为初始的 数字
				String numKeeper = ch+"";
				//定义帮助扫描的变量
				char chKeeper;
				for(int j = i+1;j < expression.length()-1;j++) {
					//循环扫描接下来的变量
					chKeeper = expression.substring(j, j + 1).charAt(0);
					System.out.println("chKeeper"+chKeeper);
					//是符号则中断循环
					if (isOpertion(chKeeper)) {
						System.out.println("断开循环了");
						break;
					}else {
						//不是符号则可以的将数字进行累加拼接
						numKeeper += chKeeper;
						System.out.println("numKeeper"+numKeeper);
						i++;
					}
				}
				//将最终结果入栈
				numStack.add(Integer.parseInt(numKeeper));
			}	
		}
		System.out.println("开始计算结果");
		while (true) {
			//操作符栈为空，数字栈的最后一个就是结果
			if (operStack.isEmpty()) {
				break;
			}
			num1 = numStack.pop();
			num2 = numStack.pop();
			res = calculate(num1, num2, operStack.pop());
			numStack.add(res);
		}
		System.out.println(expression+"="+numStack.peek());
		numStack.list();
	}

	private static int calculate(int num1, int num2, int oper) {
		switch (oper) {
		case '+':
			return num1 + num2;
		case '-':
			return num2 - num1;
		case '*':
			return num1 * num2;
		case '/':
			return num2 / num1;	
		default:
			return 0;
		}
	
	}

	private static boolean needPopOpertion(char stackHead, char ch) {
		int headPriority = getPriority(stackHead);
		int chPriority = getPriority(ch);
		return chPriority <= headPriority;
	}

	private static int getPriority(char stackHead) {
		if (stackHead == '*' || stackHead == '/') {
			return 1;
		}else if (stackHead == '+' || stackHead == '-') {
			return 0;
		}
		return -1;
	}

	private static boolean isOpertion(char ch) {
		if (ch == '+'|| ch == '-' || ch == '*'|| ch == '/') {
			return true;
		}
		return false;
	}

}
